package by.dkusch.aircompany.webapp.page.staff.panel;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.datetime.StyleDateConverter;
import org.apache.wicket.datetime.markup.html.basic.DateLabel;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;

import by.dkusch.aircompany.model.filter.FilterCrew;
import by.dkusch.aircompany.model.tables.Crew;
import by.dkusch.aircompany.model.tables.Crew_;
import by.dkusch.aircompany.services.CrewService;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.common.AddNewItemPanel;
import by.dkusch.aircompany.webapp.page.base.common.CommonActionPanel;
import by.dkusch.aircompany.webapp.page.base.common.EntityDataProviderFilter;
import by.dkusch.aircompany.webapp.page.base.common.SelectorActionPanel;
import by.dkusch.aircompany.webapp.page.base.enums.SelectorType;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.staff.CrewPage;

public class CrewListPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private CrewService crewService;
	private EntityDataProviderFilter<Long, Crew, FilterCrew> crewDataProvider;

	public CrewListPanel(String id, int itemsPerPage, final boolean choiseFl) {
		super(id);

		crewDataProvider = new EntityDataProviderFilter<Long, Crew, FilterCrew>(crewService);
		crewDataProvider.setSort(Crew_.code, SortOrder.ASCENDING);
		crewDataProvider.setFilterState(new FilterCrew());

		setOutputMarkupId(true);

		DataView<Crew> dataView = new DataView<Crew>("crewList", crewDataProvider, itemsPerPage) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<Crew> item) {
				final Crew crew = item.getModelObject();
				item.add(new CheckBox("arcFl").setEnabled(false));
				item.add(new Label("code"));
				item.add(new Label("name"));
				item.add(new DateLabel("startDate", new StyleDateConverter("S-", false)));
				item.add(new DateLabel("endDate", new StyleDateConverter("S-", false)));
				item.add(new Label("description"));
				if (choiseFl) {
					item.add(new SelectorActionPanel<Long>("action-panel", crew.getId(), SelectorType.CREW));
				} else {
					item.add(new CommonActionPanel<Long, Crew, CrewPage>("action-panel", crewService, crew, CrewPage.class, CrewListPanel.this));
				}

			}
		};

		FilterForm<FilterCrew> filterForm = new FilterForm<FilterCrew>("filterForm", crewDataProvider);
		filterForm.add(new TextField<String>("codeF", PropertyModel.<String> of(crewDataProvider, "filterState.code")));
		filterForm.add(new TextField<String>("nameF", PropertyModel.<String> of(crewDataProvider, "filterState.name")));

		final WebMarkupContainer divTable = new WebMarkupContainer("div-table");
		final PagingNavigator pagingNavigator = new PagingNavigator("paging", dataView);
		divTable.setOutputMarkupId(true);
		pagingNavigator.setOutputMarkupId(true);

		filterForm.add(new AjaxButton("crew-search", filterForm) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so that it is hidden
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
				target.add(pagingNavigator);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so errors are shown
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
			}
		});

		divTable.add(dataView);
		AddNewItemPanel<Long, Crew, CrewPage> addNewItemPanel = new AddNewItemPanel<Long, Crew, CrewPage>("new-panel", new Crew(), CrewPage.class);
		if (choiseFl) {
			addNewItemPanel.setVisible(false);
		}
		divTable.add(addNewItemPanel);

		divTable.add(new OrderByBorder<SingularAttribute<Crew, ?>>("sortByName", Crew_.name, crewDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Crew, ?>>("sortByCode", Crew_.code, crewDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Crew, ?>>("sortByDStart", Crew_.startDate, crewDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Crew, ?>>("sortByDEnd", Crew_.endDate, crewDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Crew, ?>>("sortByDescription", Crew_.description, crewDataProvider));

		filterForm.add(divTable);
		add(pagingNavigator);
		add(filterForm);


	}

}
